﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace BGCMS.Models
{
    public class BGRepository : IBGRepository
    {
        public BGDataContext db = new BGDataContext();

        /// <summary>
        /// Function that saves changes made to the tables in the database
        /// </summary>
        #region Saver

        public void SaveChanges()
        {
            db.SubmitChanges();
        }

        #endregion

        /// <summary>
        /// Returns an item or a collection of items as specified in the function names
        /// </summary>
        #region Getters

        public IEnumerable<Article> GetAllArticles()
        {
            return db.Articles;
        }
        
        public IEnumerable<Keyword> GetKeywords()
        {
            return db.Keywords;
        }

        public IEnumerable<KeywordsInArticle> GetKeywordsInArticle()
        {
            return db.KeywordsInArticles;
        }

        public IEnumerable<MenuItem> GetMenuItems()
        {
            return db.MenuItems;
        }
        
        public IEnumerable<Question> GetAllQuestions()
        {
            return db.Questions;
        }
        
        public IEnumerable<Specialist> GetAllSpecialist()
        {
            return db.Specialists;
        }

        public IEnumerable<Child> GetChildren()
        {
            return db.Childs;
        }

        public IEnumerable<HealthLog> GetHealthLogs()
        {
            return db.HealthLogs;
        }

        public IEnumerable<VaccinationLog> GetVaccinationLogs()
        {
            return db.VaccinationLogs;
        }

        public IEnumerable<AppointmentLog> GetAppointmentLogs()
        {
            return db.AppointmentLogs;
        }

        public IEnumerable<MaturityLog> GetMaturityLog()
        {
            return db.MaturityLogs;
        }

        public IEnumerable<aspnet_User> GetAllUsers()
        {
            return db.aspnet_Users;
        }

        public IEnumerable<aspnet_Membership> GetAllMembers()
        {
            return db.aspnet_Memberships;
        }

        public IEnumerable<ArticleType> GetAllArticleTypes()
        {
            return db.ArticleTypes;
        }

        // The following two functions contain some logic:
        public IQueryable<aspnet_User> GetBannedUsers()
        {
            return db.aspnet_Memberships.Where(x => x.IsApproved == false).Select(x => x.aspnet_User);
        }

        public IQueryable<aspnet_User> GetActiveUsers()
        {
            return db.aspnet_Memberships.Where(x => x.IsApproved == true).Select(x => x.aspnet_User);
        }

        #endregion

        /// <summary>
        /// Inserts new values into the tables in the database
        /// </summary>
        #region Adders

        public void UpdateKeyword(Keyword k)
        {
            db.Keywords.InsertOnSubmit(k);
        }

        public void UpdateArticle(Article a)
        {
            db.Articles.InsertOnSubmit(a);
        }

        public void UpdateQuestion(Question q)
        {
            db.Questions.InsertOnSubmit(q);
        }

        public void UpdateSpecialist(Specialist s)
        {
            db.Specialists.InsertOnSubmit(s);
        }

        public void AddKeywordsInArticle(KeywordsInArticle k)
        {
            db.KeywordsInArticles.InsertOnSubmit(k);
        }

        public void AddChild(Child c)
        {
            db.Childs.InsertOnSubmit(c);
        }

        public void AddHealthLogItem(HealthLog h)
        {
            db.HealthLogs.InsertOnSubmit(h);
        }

        public void AddVaccintaionLogItem(VaccinationLog v)
        {
            db.VaccinationLogs.InsertOnSubmit(v);
        }

        public void AddAppointment(AppointmentLog a)
        {
            db.AppointmentLogs.InsertOnSubmit(a);
        }

        #endregion

        /// <summary>
        /// Deletes an item or a collection of items from the tables in the database
        /// </summary>
        #region Deleters

        public void DeleteHealthLogItem(HealthLog h)
        {
            db.HealthLogs.DeleteOnSubmit(h);
        }

        public void DeleteVaccinationLogItem(VaccinationLog v)
        {
            db.VaccinationLogs.DeleteOnSubmit(v);
        }
        
        public void DeleteAppointment(AppointmentLog a)
        {
            db.AppointmentLogs.DeleteOnSubmit(a);
        }

        public void DeleteKeywordsInArticles(KeywordsInArticle k)
        {
            db.KeywordsInArticles.DeleteOnSubmit(k);
        }

        #endregion
    }
}